//到货通知单
$(function(){	
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		$('#product_goodsDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#product_goodsDatagrid').datagrid({ 
			queryParams:{
				goods_no:goods_no,
				goods_name:goods_name,
				bar_code:bar_code
			}
		});
	})
	//更新行数据原料
	function productordergoodsUpdateActions(value){
		$.post(productordergoodsEditUrl,{
			id:value.id,
			weight:value.weight
			},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};
	//更新行数据辅料
	function productordergoodsFUpdateActions(value){
		$.post(productordergoodsEditUrl,{
			id:value.id,
			weight:value.weight
			},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};
	//原料
	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#productordergoodsDatagrid').datagrid('validateRow', editIndex)){
			$('#productordergoodsDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑原料
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#productordergoodsDatagrid').datagrid('selectRow', index);
				$('#productordergoodsDatagrid').datagrid('endEdit', editIndex);
				$('#productordergoodsDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#productordergoodsDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	//辅料
	function endEditingF(){
		if (editIndex == undefined){return true}
		if ($('#productordergoodsFDatagrid').datagrid('validateRow', editIndex)){
			$('#productordergoodsFDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑辅料
	function onDblClickRowF(index){
		if (editIndex != index){
			if (endEditingF()){
				$('#productordergoodsFDatagrid').datagrid('selectRow', index);
				$('#productordergoodsFDatagrid').datagrid('endEdit', editIndex);
				$('#productordergoodsFDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#productordergoodsFDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#productordergoodsDatagrid').datagrid({
		//url:getproductordergoodsUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getproductordergoodsUrl,
		title:'原料列表',
		fitColumns:true,
		rownumbers:true,
		pageSize:155,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'goods_no',title:'原料编码',sortable:true},
		{field:'goods_name',title:'原料名称',sortable:true},
		{field:'source_no',title:'料号',sortable:true},
		{field:'goods_spec',title:'规格',sortable:true},
		{field:'current_type',title:'币制',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getCoinUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.coin_name;
				}
		},
		{field:'price',title:'单价',sortable:true,
				formatter:function(value,row,index){
					var num=new Number(value);
					return num.toFixed(2);
				}
		},
		{field:'weight',title:'主单位数量',sortable:true},
		{field:'conforming_p',title:'合格品数量',sortable:true},
		{field:'nonconforming_p',title:'残次品数量',sortable:true},
		{field:'scrap_one',title:'报废品数量',sortable:true},
		{field:'unit1',title:'主单位',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getUnitUrl,data:{code:value},async:false});//ajax动态获取物流备案公司名称
					var info=eval('('+htmlobj.responseText+')');
					return info.unit_name;
				}
		},
		{field:'source',title:'原产国',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getCountryUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.country_name;
				}
		},
		{field:'goods_spec',title:'规格',sortable:true,
				formatter:function(value,row,index){
					return row.goods_price*row.weight;
				}
		}
		]],
		queryParams:{
			id:plan_id
		},
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					row.editing = false;
					productordergoodsUpdateActions(row);
		},
	});
});
product_indexgoodsObj={
	showAll:function(){
		$('#product_goodsDatagrid').datagrid({
				queryParams:''
		});
		$('.datagrid-pager-form').find('.datagrid-pager').css({'position':'relative','bottom':'0'});
	},
	addBar:function(status){

		var indexgoodsRow=$('#product_goodsDatagrid').datagrid('getSelections');
        if (indexgoodsRow.length>0){
        	$.messager.confirm('提示','真的要添加这'+indexgoodsRow.length+'个商品吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<indexgoodsRow.length;i++){
                		ids.push(indexgoodsRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productaddgoodsUrl,{id:id,plan_id:plan_id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#productordergoodsAddDialog').dialog('close');
								$('#productordergoodsDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
};
productordergoodsObj={
	showAll:function(){//显示全部数据
		$('#productordergoodsDatagrid').datagrid({ queryParams:{id:plan_id,goods_cate:2}});
	},
	add:function(url){//新增数据
		$('#productordergoodsAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if(!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#productordergoodsAddDialog').dialog('close');  
                    });
                }else{
					$.messager.alert('提示信息',result.message,'info',function(r){
                    	$('#productordergoodsAddDialog').dialog('close');       
						$('#productordergoodsDatagrid').datagrid('reload'); 
                    });
                }
            },
		});
	},
	save:function(url){//编辑后保存数据
		$('#productordergoodsEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#productordergoodsEditDialog').dialog('close');
                    });
                }else{
                    $('#productordergoodsEditDialog').dialog('close');      
                    $('#productordergoodsDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#productordergoodsDatagrid').datagrid('validateRow', editIndex)){
			$('#productordergoodsDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if(endEditing == 1){
			$('#productordergoodsDatagrid').datagrid('acceptChanges');
		}
	},
	reloadBar:function(){//刷新数据
		$('#productordergoodsDatagrid').datagrid('reload');
	},
	recycle:function(){//查看回收站数据
		$('#productordergoodsDatagrid').datagrid({ queryParams:{id:plan_id,status:-1,goods_cate:2}});
	},
	addBar:function(){//显示添加框
		 $('#productordergoodsAddDialog').dialog({
			// minimizable: true,//最小化，默认false  
	//         maximizable: true, //最大化，默认false  
	//         collapsible: true, //可折叠，默认false
			//按钮  
            buttons: [{
                text: '确定添加',
                iconCls: 'icon-ok',
                handler: function () {
					product_indexgoodsObj.addBar(status);
                }
            }, {
                text: '取消',
                handler: function () {
                    $('#productordergoodsAddDialog').dialog('close');
                }
            }]
		});
		$('#productordergoodsAddDialog').dialog('open').dialog('setTitle','商品列表');
		// $('#product_goodsDatagrid').datagrid('reload');
		$('#product_goodsDatagrid').datagrid({
			url:getbooksUrl,
			fitColumns:true,
			rownumbers:true,
			pageSize:25,
			pageNumber:1,
			pageList:[5,10,15,20,25,30,40,50],
			columns:[[
			{field:'id',title:'id',checkbox:true},
			{field:'manua_id',title:'账册编码',sortable:true},
			{field:'source_no',title:'料号',sortable:true},
			{field:'goods_no',title:'商品编码',sortable:true},
			{field:'goods_name',title:'商品名称',sortable:true},
			{field:'hs_code',title:'海关编码',sortable:true},
			{field:'goods_spec',title:'规格',sortable:true},
			{field:'current_type',title:'币制',sortable:true},
			{field:'source',title:'产销国',sortable:true},
			{field:'unit1',title:'第一单位',sortable:true},
			{field:'unit2',title:'第二单位',sortable:true}
			]],
			queryParams:{
				goods_cate:2,
				page:1
			},
			onLoadError:function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					var tab=$('#Tabs').tabs('getSelected');
					var index=$('#Tabs').tabs('getTabIndex',tab);
					$('#Tabs').tabs('close',index);
				});
			},
			pagination:true,
			fit:true,
		});
		$('.datagrid-pager-form').find('.datagrid-pager').css({'position':'relative','bottom':'0'});
	},
	editBar:function(){//显示编辑框
		var productordergoodsRow=$('#productordergoodsDatagrid').datagrid('getSelections');
        if (productordergoodsRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(productordergoodsRow.length==1){            	
        	$('#productordergoodsEditDialog').dialog('open').dialog('setTitle','编辑');
            $('#productordergoodsEditForm').form('load',productordergoodsRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var productordergoodsRow=$('#productordergoodsDatagrid').datagrid('getSelections');
        if (productordergoodsRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+productordergoodsRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<productordergoodsRow.length;i++){
                		ids.push(productordergoodsRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productordergoodsDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#productordergoodsAddDialog').dialog('close');       
								$('#productordergoodsDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	clearBar:function(){//删除/清空回收站数据
		var productordergoodsRow=$('#productordergoodsDatagrid').datagrid('getSelections');
        if (productordergoodsRow.length>0){
        	$.messager.confirm('清空提示','真的要清空这'+productordergoodsRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<productordergoodsRow.length;i++){
                		ids.push(productordergoodsRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productordergoodsClearUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#productordergoodsAddDialog').dialog('close');       
								$('#productordergoodsDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	undoBar:function(){//恢复回收站数据
		var productordergoodsRow=$('#productordergoodsDatagrid').datagrid('getSelections');
        if (productordergoodsRow.length>0){
        	$.messager.confirm('恢复提示','真的要恢复这'+productordergoodsRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<productordergoodsRow.length;i++){
                		ids.push(productordergoodsRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productordergoodsUndoUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#productordergoodsAddDialog').dialog('close');       
								$('#productordergoodsDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
};